<template>
    <div>
        <h3>房型</h3>
        房型名称<input type="text" v-model="queryinfo.housename"/>
        <input type="button" value="查询" class="btn btn-info" @click="show"/>
        <input type="button" value="批量删除" class="btn btn-danger" @click="delall"/>
        <input type="button" value="批量修改" class="btn btn-danger" @click="update"/>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><input type="checkbox" v-model="checked" @click="checkall"/></td>
                    <td>ID</td>
                    <td>房型名称</td>
                    <td>是否有窗</td>
                    <td>房型面积</td>
                    <td>图片</td>
                    <td>房型价格</td>
                    <td>状态</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.list" :key="item.Id">
                    <td><input type="checkbox" v-model="ids" :value="item.Id" @change="checkone"/></td>
                    <td>{{ item. Id}}</td>
                    <td>{{ item.HouseTypeName }}</td>
                    <td>{{ item.IsWindow?"有窗":"无窗" }}</td>
                    <td>{{ item.Area }}</td>
                    <td><img :src="item.Photo" alt="" width="100" height="100"/></td>
                    <td>{{ item.Price }}</td>
                    <td>{{ item.State==0?"禁用":"启用" }}</td>
                    <td>
                        <input type="button" v-if="item.State==0" value="上架" class="btn btn-success" @click="upd(item.Id)"/>
                        <input type="button" v-else value="下架" class="btn btn-warning" @click="upd(item.Id)"/>
                        <input type="button" value="详情" class="btn btn-success" @click="delxian(item.Id)"/>
                    </td>
                </tr>
            </tbody>
        </table>
        <div>
            <span>共{{ data.tocount }}条</span>
            <span>共{{ data.topage }}页</span>
            <span>每页{{ queryinfo.pgesize }}条</span>
            <span>当前是{{ queryinfo.pageindex }}/{{data.topage}}页</span>
            <select v-model="queryinfo.pgesize" @change="show">
                <option :value="item" v-for="item in arr" :key="item">每页{{ item }}条</option>
            </select>
            <input type="button" value="首页" @click="fanye(1)"/>
            <input type="button" value="上一页" @click="fanye(queryinfo.pageindex-1)"/>
            <input type="button" value="下一页" @click="fanye(queryinfo.pageindex+1)"/>
            <input type="button" value="尾页" @click="fanye(data.topage)"/>
        </div>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive, onMounted}from 'vue';
import axios from 'axios';
import moment from 'moment';
import {useRouter,useRoute}from 'vue-router'
import { idText } from 'typescript';
const router=useRouter();
const route=useRoute();

let queryinfo:any=reactive({
    pageindex:1,
    pgesize:2,
    housename:'',
})

let data:any=reactive({
    list:[],
    tocount:0,
    topage:0
})

let checked:any=ref([false]);

let ids=ref([]);

let arr=ref([2,5,7,9]);

onMounted(()=>{
    getdist();
})

const delxian=(id:any)=>{
    router.push({
        path:'/xiangqing',
        query:{id:id},
    })
}

const upd=(ids:any)=>{
    if(!confirm("确认修改吗")){
        return;
    }
    axios({
        url:'/api/House/Getupdate',
        method:'put',
        params:{ids:ids}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('修改成功');
            checked.value=false;
            getdist();
        }
        else{
            alert('修改失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const update=()=>{
    if(!confirm("确认修改吗")){
        return;
    }
    axios({
        url:'/api/House/Getupdate',
        method:'put',
        params:{ids:ids.value.toString()}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('修改成功');
            checked.value=false;
            getdist();
        }
        else{
            alert('修改失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const delall=()=>{
    if(!confirm("确认删除吗")){
        return;
    }
    axios({
        url:'/api/House/GetDel',
        method:'delete',
        params:{ids:ids.value.toString()}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('删除成功');
            checked.value=false;
            getdist();
        }
        else{
            alert('删除失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const checkall=()=>{
    checked.value=!checked.value;
    if(checked.value==true){
        ids.value=data.list.map((item:any)=>{
            return item.Id;
        })
    }
    else{
        return ids.value=[];
    }
}

const checkone=()=>{
    if(ids.value.length==data.list.length){
        checked.value=true;
    }
    else{
        checked.value=false;
    }
}



const show=()=>{
    queryinfo.pageindex=1;
    getdist();
}

const fanye=(num:any)=>{
    if(num<1){
        queryinfo.pageindex=1;
        return;
    }
    if(num>data.topage){
        queryinfo.pageindex=data.topage;
        return;
    }
    queryinfo.pageindex=num;
    getdist();
}

const getdist=()=>{
    axios({
        url:'/api/House/GetHouseList',
        method:'get',
        params:queryinfo
    })
    .then(res=>{
        console.log(res);
        data.list=res.data.list;
        data.tocount=res.data.tocount;
        data.topage=res.data.topage;
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>

<style scoped>
span{
    margin-left: 10px;
}
input[type="button"]{
    margin-left: 20px;
}
</style>